//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj账户验证

#import "AccountVerificationVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "RegisterViewController.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
#import "CaptchaView.h"
#import "EnterCodeController.h"
#import "MJConfirmLoadingView.h"

@interface AccountVerificationVC ()
@property (weak, nonatomic) IBOutlet UILabel *currentPhoneLabel;
@property (weak, nonatomic) IBOutlet MJConfirmLoadingView *confirmButton;

@end

@implementation AccountVerificationVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
   
    NSString *email = self.email;
    if(email){
        self.currentPhoneLabel.text = [NSString stringWithFormat:@"%@\n%@",MJUILocalizedString(@"Click Get Code to send verification code to:"),email];
    }else{
        NSString *phone = self.phoneNumber;
        if(phone){
            NSString *currentPhone = [NSString stringWithFormat:@"+%@ %@",UserManager.shared.userModel.countryCode,self.phoneNumber];
            self.currentPhoneLabel.text = [NSString stringWithFormat:@"%@\n%@",MJUILocalizedString(@"Click Get Code to send verification code to:"),currentPhone];
        }
    }
    
    MJWeakSelf
    self.confirmButton.handleContinueBlock = ^{
        [weakSelf handleConfirmButtonClick];

    };
    [self.confirmButton.continueButton setTitle:MJUILocalizedString(@"Get Code") forState:0];
}

#pragma mark - 方法
- (void)requestData{
    
}

- (void)handleConfirmButtonClick{
    [self.confirmButton.loadingView stopAnimation];

    MJWeakSelf
    [CaptchaView showWithType: puzzle CompleteBlock:^(NSString *  result) {
        
        [weakSelf.confirmButton.loadingView startAnimation];
        
        NSString *email =self.email;
        if(email){
            NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{
                @"email": email,
                @"code": result,
            }];
            [MJHttpNetWorking requestWithParams:parms requestUri:captcha_sendEmail requestType:RequestTypePost showLoading:false success:^(MJBaseModel *model) {
                [weakSelf.confirmButton.loadingView stopAnimation];

                if (model.code ==  200){
                    EnterCodeController *vc = [[EnterCodeController alloc]init];
                    vc.email = email;
                    vc.captchaCodeType = CaptchaCodeTypeEmailForgotPassword;
                    [weakSelf.navigationController pushViewController:vc animated:true];
                }
                
            } failure:^(NSError *error) {
                [weakSelf.confirmButton.loadingView stopAnimation];

            }];
        }else{
            NSString *phone = weakSelf.phoneNumber;
            if(phone){
                NSDictionary *countryAreaCode = [HealthHelper getCountryWithCountryCode:UserManager.shared.userModel.countryCode];
                NSLog(@"result=======================%@",result);
                NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{
                    @"countryCode": UserManager.shared.userModel.countryCode,
                    @"phoneNumber": weakSelf.phoneNumber,
                    @"region": countryAreaCode[@"ISO"],
                    @"code": result,
                }];
                [MJHttpNetWorking requestWithParams:parms requestUri:captcha_sendSms requestType:RequestTypePost showLoading:false success:^(MJBaseModel *model) {
                    [weakSelf.confirmButton.loadingView stopAnimation];

                    if (model.code ==  200){
                        EnterCodeController *vc = [[EnterCodeController alloc]init];
                        vc.phone = weakSelf.phoneNumber;
                        vc.countryAreaCode = countryAreaCode;
                        vc.captchaCodeType = CaptchaCodeTypeForgotPassword;
                        [weakSelf.navigationController pushViewController:vc animated:true];
                    }
                    
                } failure:^(NSError *error) {
                    [weakSelf.confirmButton.loadingView stopAnimation];

                }];
            }
        }
       
        
    }];
    
}


#pragma mark - 代理

#pragma mark - 懒加载

@end
